<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Reset Password</title>
    <script type="text/javascript">
        async function requestVerificationCode() {
            const email = localStorage.getItem('email');
            if (!email) {
                alert("Email not found in local storage.");
                return;
            }

            const response = await fetch('http://localhost:8080/agent/request_reset_password', {
                method: 'POST',
                <!-- 这里由于是后端静态html，手动添加token -->
                headers: {'Content-Type': 'application/json','Authorization':""},
                body: JSON.stringify({email: email})
            });

            const data = await response.json();
            if (response.ok) {
                alert(data.message || "Verification code sent successfully");
            } else {
                alert(data.message || "Failed to send verification code");
            }
        }

        function validatePasswords() {
            const newPassword = document.getElementById('new_password').value;
            const confirmPassword = document.getElementById('confirm_password').value;

            if (newPassword !== confirmPassword) {
                alert("Passwords do not match.");
                return false;
            }
            return true;
        }

        async function submitForm(event) {
            event.preventDefault(); // 防止默认的表单提交行为

            if (!validatePasswords()) return;

            const token = document.getElementById('token').value;
            const newPassword = document.getElementById('new_password').value;

            const response = await fetch('/reset_password', {
                method: 'POST',
                headers: {'Content-Type': 'application/json'},
                body: JSON.stringify({token: token, new_password: newPassword})
            });

            const data = await response.json();
            if (response.ok) {
                alert(data.message || "Password reset successful");
            } else {
                alert(data.message || "Failed to reset password");
            }
        }
    </script>
</head>
<body>
<h1>Password Reset</h1>
<form onsubmit="submitForm(event)">

    <!-- 新密码字段 -->
    <label for="new_password">New Password:</label><br>
    <input type="password" id="new_password" name="new_password" required><br>

    <!-- 确认新密码字段 -->
    <label for="confirm_password">Confirm New Password:</label><br>
    <input type="password" id="confirm_password" name="confirm_password" required><br><br>
    
    <!-- 验证码字段 -->
    <label for="token">Verification Code:</label><br>
    <input type="text" id="token" name="token" required><br>
    <button type="button" onclick="requestVerificationCode()">Get Verification Code</button><br><br>

    <input type="submit" value="Reset Password">
</form>
</body>
</html>